package com.hupu.jrs.ui.content

import android.app.Fragment
import android.app.FragmentManager
import android.content.Context
import android.content.Intent
import android.support.v13.app.FragmentPagerAdapter
import android.support.v4.view.ViewPager
import android.view.Gravity
import android.view.View
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import com.github.clans.fab.FloatingActionButton
import com.github.clans.fab.FloatingActionMenu
import com.hupu.jrs.R
import com.hupu.jrs.injector.HasComponent
import com.hupu.jrs.ui.BaseSwipeBackActivity
import com.hupu.jrs.ui.login.LoginActivity
import com.hupu.jrs.ui.post.PostActivity
import com.hupu.jrs.ui.report.ReportActivity
import com.hupu.jrs.util.DisplayUtil
import com.hupu.jrs.util.ResourceUtil
import com.hupu.jrs.widget.PagePicker
import com.hupu.jrs.widget.ProgressBarCircularIndeterminate
import com.hupu.jrs.widget.VerticalViewPager
import javax.inject.Inject
import butterknife.BindView
import butterknife.ButterKnife
import butterknife.OnClick
import com.hupu.jrs.Constants

/**
 * Created by sll on 2016/3/9.
 */
class ContentActivity : BaseSwipeBackActivity(), ContentContract.View, PagePicker.OnJumpListener, ViewPager.OnPageChangeListener, HasComponent<ContentComponent> {

    override val isApplyStatusBarTranslucency: Boolean
        get() = isApplyStatusBarTranslucencyImp()
    override val isApplyStatusBarColor: Boolean
        get() = isApplyStatusBarColorImp()

    @BindView(R.id.viewPager)
    internal var viewPager: VerticalViewPager? = null
    @BindView(R.id.tvLoading)
    internal var tvLoading: TextView? = null
    @BindView(R.id.progress_container)
    internal var progressContainer: LinearLayout? = null
    @BindView(R.id.rlProgress)
    internal var rlProgress: RelativeLayout? = null
    @BindView(R.id.floatingComment)
    internal var floatingComment: FloatingActionButton? = null
    @BindView(R.id.floatingReport)
    internal var floatingReport: FloatingActionButton? = null
    @BindView(R.id.floatingCollect)
    internal var floatingCollect: FloatingActionButton? = null
    @BindView(R.id.floatingShare)
    internal var floatingShare: FloatingActionButton? = null
    @BindView(R.id.floatingMenu)
    internal var floatingMenu: FloatingActionMenu? = null
    @BindView(R.id.tvPre)
    internal var tvPre: TextView? = null
    @BindView(R.id.tvPageNum)
    internal var tvPageNum: TextView? = null
    @BindView(R.id.tvNext)
    internal var tvNext: TextView? = null
    @BindView(R.id.rlPage)
    internal var rlPage: RelativeLayout? = null
    @BindView(R.id.rlError)
    internal var rlError: RelativeLayout? = null
    @BindView(R.id.tvError)
    internal var tvError: TextView? = null
    @BindView(R.id.frameLayout)
    internal var frameLayout: FrameLayout? = null
    @BindView(R.id.progress_view)
    internal var progressBar: ProgressBarCircularIndeterminate? = null

    @Inject
    internal var mPresenter: ContentPresenter? = null

    private var fid: String? = null
    private var tid: String? = null
    private var page: Int = 0
    private var pid: String? = null

    private var mPagePicker: PagePicker? = null
    private var mAdapter: MyAdapter? = null


    override lateinit var component: ContentComponent
    override var component: ContentComponent
        get() = component
        set(value) {super.component = value}

    override fun initContentView(): Int {
        return R.layout.activity_content
    }

    override fun initInjector() {
        component = DaggerContentComponent.builder()
                .applicationComponent(applicationComponent)
                .activityModule(activityModule)
                .contentModule(ContentModule())
                .build()
        component!!.inject(this)
    }

    override fun initUiAndListener() {
        ButterKnife.bind(this)
        mPresenter!!.attachView(this)
        fid = intent.getStringExtra("fid")
        tid = intent.getStringExtra("tid")
        page = intent.getIntExtra("page", 1)
        pid = intent.getStringExtra("pid")
        initPicker()
        initFloatingButton()
        viewPager!!.setOnPageChangeListener(this)
        progressBar!!.setBackgroundColor(ResourceUtil.getThemeColor(this))
        mPresenter!!.onThreadInfoReceive(tid.toString(), fid.toString(), pid.toString(), page)
    }

    private fun initPicker() {
        mPagePicker = PagePicker(this)
        mPagePicker!!.setOnJumpListener(this)
    }

    private fun initFloatingButton() {
        ResourceUtil.setFabMenuColor(this, floatingMenu!!)
        ResourceUtil.setFabBtnColor(this, floatingComment!!)
        ResourceUtil.setFabBtnColor(this, floatingCollect!!)
        ResourceUtil.setFabBtnColor(this, floatingShare!!)
        ResourceUtil.setFabBtnColor(this, floatingReport!!)
    }

    fun isApplyStatusBarTranslucencyImp(): Boolean {
        return false
    }

    fun isApplyStatusBarColorImp(): Boolean {
        return false
    }

    override fun showLoading() {
        rlProgress!!.visibility = View.VISIBLE
        viewPager!!.visibility = View.GONE
        rlError!!.visibility = View.GONE
    }

    override fun hideLoading() {
        rlProgress!!.visibility = View.GONE
        viewPager!!.visibility = View.VISIBLE
        rlError!!.visibility = View.GONE
    }

    override fun renderContent(page: Int, totalPage: Int) {
        mAdapter = MyAdapter(fragmentManager, totalPage)
        viewPager!!.adapter = mAdapter
        viewPager!!.currentItem = page - 1
        onUpdatePager(page, totalPage)
    }

    override fun setCurrentItem(index: Int) {
        viewPager!!.currentItem = index
    }

    override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}

    override fun onPageSelected(position: Int) {
        mPresenter!!.updatePage(position + 1)
    }

    override fun onUpdatePager(page: Int, totalPage: Int) {
        mPagePicker!!.setMin(1)
        mPagePicker!!.setMax(totalPage)
        mPagePicker!!.setValue(page)
        tvPageNum!!.text = page.toString() + "/" + totalPage
        if (page == 1) {
            tvPre!!.setTextColor(resources.getColor(R.color.base_text_gray))
            tvPre!!.isClickable = false
        } else {
            tvPre!!.setTextColor(resources.getColor(R.color.blue))
            tvPre!!.isClickable = true
        }

        if (page == totalPage) {
            tvNext!!.setTextColor(resources.getColor(R.color.base_text_gray))
            tvNext!!.isClickable = false
        } else {
            tvNext!!.setTextColor(resources.getColor(R.color.blue))
            tvNext!!.isClickable = true
        }
    }

    override fun onPageScrollStateChanged(state: Int) {

    }

    inner class MyAdapter(fm: FragmentManager, private val totalPage: Int) : FragmentPagerAdapter(fm) {

        init {
            notifyDataSetChanged()
        }

        override fun getItem(position: Int): Fragment {
            return ContentFragment.newInstance(fid.toString(), tid.toString(), pid.toString(), position + 1)
        }

        override fun getCount(): Int {
            return totalPage
        }
    }

    override fun isCollected(isCollected: Boolean) {
        floatingCollect!!.setImageResource(
                if (isCollected) R.drawable.ic_menu_star else R.drawable.ic_menu_star_outline)
        floatingCollect!!.labelText = if (isCollected) "取消收藏" else "收藏"
    }

    override fun onError(error: String) {
        tvError!!.text = error
        rlProgress!!.visibility = View.GONE
        viewPager!!.visibility = View.GONE
        rlError!!.visibility = View.VISIBLE
    }

    override fun onToggleFloatingMenu() {
        floatingMenu!!.toggle(true)
    }

    override fun showLoginUi() {
        LoginActivity.startActivity(this)
    }

    override fun showReportUi() {
        ReportActivity.startActivity(this, tid.toString(), "")
    }

    override fun showPostUi(title: String) {
        PostActivity.startActivity(this, Constants.TYPE_COMMENT, fid.toString(), tid.toString(), "", title)
    }

    override fun OnJump(page: Int) {
        mPresenter!!.onPageSelected(page)
    }

    @OnClick(R.id.floatingComment)
    internal fun setFloatingCommentClick() {
        mPresenter!!.onCommendClick()
    }

    @OnClick(R.id.floatingShare)
    internal fun floatingShare() {
        mPresenter!!.onShareClick()
    }

    @OnClick(R.id.floatingReport)
    internal fun floatingReport() {
        mPresenter!!.onReportClick()
    }

    @OnClick(R.id.floatingCollect)
    internal fun floatingCollect() {
        mPresenter!!.onCollectClick()
    }

    @OnClick(R.id.tvPre)
    internal fun tvPre() {
        mPresenter!!.onPagePre()
    }

    @OnClick(R.id.tvNext)
    internal fun tvNext() {
        mPresenter!!.onPageNext()
    }

    @OnClick(R.id.tvPageNum)
    internal fun tvPageNum() {
        if (mPagePicker!!.isShowing) {
            mPagePicker!!.dismiss()
        } else {
            mPagePicker!!.showAtLocation(frameLayout, Gravity.BOTTOM, 0, DisplayUtil.dip2px(this, 40f))
        }
    }

    @OnClick(R.id.btnReload)
    internal fun btnReload() {
        mPresenter!!.onReload()
    }

    override fun onDestroy() {
        super.onDestroy()
        mPresenter!!.detachView()
    }

    fun setFloatingMenuVisibility(show: Boolean) {
        if (show) {
            floatingMenu!!.showMenu(true)
        } else {
            floatingMenu!!.hideMenu(true)
        }
    }

    companion object {

        fun startActivity(mContext: Context, fid: String, tid: String, pid: String, page: Int) {
            val intent = Intent(mContext, ContentActivity::class.java)
            intent.putExtra("fid", fid)
            intent.putExtra("tid", tid)
            intent.putExtra("pid", pid)
            intent.putExtra("page", page)
            mContext.startActivity(intent)
        }
    }
}
